/*	This file is part of AstroAttack.
*
*	AstroAttack is free software; you can redistribute it and/or modify
*	it under the terms of the GNU General Public License as published by
*	the Free Software Foundation; either version 2 of the License, or
*	(at your option) any later version.
*	
*	AstroAttack is distributed in the hope that it will be useful,
*	but WITHOUT ANY WARRANTY; without even the implied warranty of
*	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
*	GNU General Public License for more details.
*	
*	You should have received a copy of the GNU General Public License
*	along with AstroAttack; if not, write to the Free Software
*	Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
*	
*	$Id: Entity.java 28 2006-12-27 20:22:54Z xdracox $
*/

/**	The AstroAttack package contains all the classes that belong to the
*	AstroAttack program.
*/
package AstroAttack;

import java.awt.Image;
import java.awt.Graphics2D;
import java.awt.geom.Point2D;
import java.awt.geom.Rectangle2D;

/**	The Entity class represents any object that can be moved and displayed
*	anywhere on the screen.
*/
public class Entity
{
	/**	The image this Entity is displayed as. */
	private Image m_image;
	/** The position of this entity. */
	private Point2D m_point;
	/**	Direction the entity is moving in. */
	private double m_direction;
	/**	The speed the entity is moving at. */
	private double m_speed;
	
	/**	The constructor initializes the Entity's image representation
	*	and it's location on the screen.
	*/
	public Entity(Image image, Point2D point)
	{
		if ( image == null )
			throw new IllegalArgumentException("Parameter image cannot be null");
		if ( point == null )
			throw new IllegalArgumentException("Parameter point cannot be null");
		
		m_image = image;
		m_point = point;
	}
	
	/**	Moves the Entity to where it should be according to the delta parameter
	*	@param	*/
	public void move(double delta)
	{
		double xSpeed = m_speed * Math.cos(Math.toRadians(m_direction));
		double ySpeed = -m_speed * Math.sin(Math.toRadians(m_direction));
		
		double newXPos = m_point.getX() + (xSpeed * delta);
		double newYPos = m_point.getY() + (ySpeed * delta);
		
		m_point.setLocation(newXPos, newYPos);
	}
	
	public void render(Graphics2D gfx)
	{
		/**	@todo	there must be better way to draw the image */
		gfx.drawImage(m_image, (int)m_point.getX(), (int)m_point.getY(), null);
	}
	
	public Rectangle2D getBounds()
	{
		double w = m_image.getWidth(null);
		double h = m_image.getHeight(null);
		double x = m_point.getX();
		double y = m_point.getY();
		
		return new Rectangle2D.Double(x, y, w, h);
	}
	
	public void setImage(Image img)
	{
		if ( img == null )
			throw new IllegalArgumentException("Parameter img cannot be null");
		m_image = img;
	}
}
